//
//  TenViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/30.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class TenViewController: UIViewController {

    let itemArray = ["点按","拖动","轻扫","旋转","捏合","长按"]
    let cellId = "ten_table_cell";
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "手势放大"
        // Do any additional setup after loading the view.
        self.onCreateUIForNavigation();
        self.onCreateUIForView();
    }
    
    //TODO:UI
    fileprivate func onCreateUIForNavigation() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItem.Style.done, target: self, action: #selector(onBackAction))
    }

    fileprivate func onCreateUIForView() {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: .plain);
        tableView.backgroundColor = .white;
        tableView.showsVerticalScrollIndicator = false;
        tableView.showsHorizontalScrollIndicator = false;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId);
        self.view.addSubview(tableView);
    }

    @objc fileprivate func onBackAction() {
        self.navigationController?.dismiss(animated: true, completion: nil);
    }
    
    fileprivate func onGetGestureType(typeName:String) -> TenGestureType {
        var type = TenGestureType.TenGestureType_Tap;
        if typeName == "点按" {
            type = TenGestureType.TenGestureType_Tap;
        }
        else if typeName == "拖动" {
            type = TenGestureType.TenGestureType_Pan;
        }
        else if typeName == "轻扫" {
            type = TenGestureType.TenGestureType_Swipe;
        }
        else if typeName == "旋转" {
            type = TenGestureType.TenGestureType_Rotation;
        }
        else if typeName == "捏合" {
            type = TenGestureType.TenGestureType_Pinch;
        }
        else if typeName == "长按" {
            type = TenGestureType.TenGestureType_LongPress;
        }
        return type;
    }
}

extension TenViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemArray.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellId)!;
        
        if self.itemArray.count > indexPath.row {
            cell.textLabel?.text = self.itemArray[indexPath.row];
        }
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.itemArray.count > indexPath.row {
            let detailVC = TenDetailViewController();
            detailVC.gestureType = self.onGetGestureType(typeName: self.itemArray[indexPath.row]);
            self.navigationController?.pushViewController(detailVC, animated: true);
        }
    }
}
